package com.guli.produce.produce.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.produce.produce.dao.CategoryDao;
import com.guli.produce.produce.entity.CategoryEntity;
import com.guli.produce.produce.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> treeList() {
        //查询所有的属性
        List<CategoryEntity> allAlist = baseMapper.selectList(null);
        //去筛选

        List<CategoryEntity> list = allAlist.stream().filter(item -> {

            //这里是获取到fid为0的
            return item.getParentCid().equals(0l);
        }).map(item -> {  //此时item中都是一级分类 筛选到的 要用map去收集起来
            //一级分类中有2,3及
            //一级的孩子用递归去查询
            item.setChildren(getChildren(item.getCatId(),allAlist));
            return item;
        }).sorted((sort1,sort2) ->{   //做排序
            return ((sort1.getSort()==null?0:sort1.getSort())-(sort2.getSort()==null?0:sort2.getSort()));
        }).collect(Collectors.toList());
        return list;
    }


    private List<CategoryEntity> getChildren(Long catId,List<CategoryEntity> allAlist){

        List<CategoryEntity> children = allAlist.stream().filter(item -> {
            return item.getParentCid().equals(catId);
        }).map(item -> {
            //此时item是二级分类
            //三级
            item.setChildren(getChildren(item.getCatId(),allAlist));
            return item;
        }).sorted((sort1,sort2) ->{
            return ((sort1.getSort()==null ? 0: sort1.getSort())-(sort2.getSort()==null?0 : sort2.getSort()));
        }).collect(Collectors.toList());
        return children;
    }

   /* @Override
    public List<CategoryEntity> treeList() {
        //查询所有的一级分类
        List<CategoryEntity> l1list = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));

        for (CategoryEntity categoryEntity : l1list) {
            List<CategoryEntity> l2list = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid",categoryEntity.getCatId()));
            categoryEntity.setChildren(l2list);
            for (CategoryEntity entity : l2list) {
                List<CategoryEntity> l3list = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", entity.getCatId()));
                entity.setChildren(l3list);
            }
        }

        return l1list;
    }*/

}